template.defaults.imports.CountdownTime = function (factEndDate) {
    var timestamp = (new Date()).valueOf();
    return factEndDate - timestamp;
};

    /* global getCookie */
    var loading = false; //状态标记
    var hasNextPage = false;
    var pageNo = 1;
    var orderState = -1;//1全部 2待审核 3待售卖
    queryList(pageNo);

    //下拉刷新
    $("#aroundBox").pullToRefresh().on("pull-to-refresh", function () {
        $("#contentTo").html("");
        pageNo = 1;
        queryList(pageNo,function(){
            $("#aroundBox").pullToRefreshDone(); // 重置下拉刷新
        });
    });

    //上拉加载更多
    $('#aroundBox').infinite().on("infinite", function () {
        if (hasNextPage){
            pageNo++;
            queryList(pageNo,function(){
            },true);
        }        
    });

    function queryList(pageNo,callback,isMore){
        if (loading) return;
        loading = true;
        var params = {};
        params.start = (pageNo - 1) * 20;
        params.limit = 20;
        params.token = getCookie('token');
        params.state = orderState;
        if (orderState == -1) {
            delete params.state;
        } 
        $.get('/antique/api/mallConsignOrder/query_list', params, function (result) {
            if (result.code == 0) {
                if (!isMore){
                    /* global template */
                    document.getElementById('contentTo').innerHTML = template('content', result.data);                
                } else {
                    $("#contentTo").append(template('content', result.data));
                }
                hasNextPage = result.data.hasNextPage;
                if (!result.data.hasNextPage) {
                    $('#loadmore').hide();
                    $('#nomore').show();
                } else {
                    $('#loadmore').show();
                    $('#nomore').hide(); 
                }
                if (callback) callback();
                loading = false;
            }

            
        });
    }

$(".weui-tab .tabs .item").click(function () {
    $(".weui-tab .tabs .item").removeClass("current");
    $(this).addClass("current");

    orderState = $(this).data('state');
    $("#contentTo").html("");
    pageNo = 1;
    queryList(pageNo,function(){
        $("#aroundBox").pullToRefreshDone(); // 重置下拉刷新
    });
    
});

function cancelReceive (orderId) {
    $.confirm("您是否确认取消寄售？", "", function() {
        $.post('/antique/api/mallConsignOrder/consign_cancel', { id:orderId,token:getCookie('token') }, function (result) {
            if (result.code == 0) {
                $.toast("操作成功！");
                $("#contentTo").html("");
                pageNo = 1;
                queryList(pageNo,function(){},true);
            } else {
             $.toast(result.message, "cancel");
            }
         });
      }, function() {
        //取消操作
      });
    
}
function myDeliver (orderId) {
    $.post('/antique/api/mallConsignOrder/consign_sendgoods', { id:orderId,token:getCookie('token') }, function (result) {
        if (result.code == 0) {
            $("#contentTo").html("");
            pageNo = 1;
            queryList(pageNo,function(){},true);
        } else {
            $.toast(result.message, "cancel");
        }
        });
   
    
}
function returnDeliver (orderId) {
    $.confirm("您是否确认已经收到货？", "", function() {
        $.post('/antique/api/mallConsignOrder/consign_back_confirm', { id:orderId,token:getCookie('token') }, function (result) {
            if (result.code == 0) {
                $.toast("操作成功！");
                $("#contentTo").html("");
                pageNo = 1;
                queryList(pageNo,function(){},true);
            } else {
             $.toast(result.message, "cancel");
            }
         });
      }, function() {
        //取消操作
      });
    
}
function getlogisticscompany(logisticsValue){
    $.ajax({
        type: 'POST',
        url: "/antique/api/sysDict/query_list",
        data: {
            type: "logisticscompany",
            "token": getCookie('token')
        },
        dataType: "json",
        success: function (result) {
            if (result.code == 0) {
                if (result.data && result.data.list && result.data.list.length > 0) {
                    var list = result.data.list;
                    for (var i = 0; i < list.length; i++) {
                        if ( logisticsValue == list[i].value){
                            $("#logisticsCompany").append("<option select value='" + list[i].value + "'>" + list[i].label + "</option>");
                        } else {
                            $("#logisticsCompany").append("<option value='" + list[i].value + "'>" + list[i].label + "</option>");
                        }
                        
                    }
                }
            }
        }
    });
    }
function logistics (orderId,logisticsCode,sendLogisticsNo) {
    getlogisticscompany(logisticsCode );
    $.modal({
      title: "填写物流信息",
      text: '<input type="hidden" id="orderId" value="' + orderId + '" />' +
      '<select class="weui-select selectss" id="logisticsCompany"><option disabled selected style=display:none;">请选择物流公司</option></select>' + 
      '<input class="inputss" type="text" id="sendLogisticsNo" value="' + sendLogisticsNo + '" placeholder="请输入快递单号" />',
      buttons: [
        { text: "确定", onClick: function(){
            var sendLogisticsCode = $("#sendLogisticsCode").val();
            var sendLogisticsNo = $("#sendLogisticsNo").val();
            var id = $("#orderId").val();
            
             if (sendLogisticsCode == ''){
                 $.toast("公司不能为空", "cancel");
                 return;
             }
             if (sendLogisticsNo == ''){
                 $.toast("单号不能为空", "cancel");
                 return;
             }
             var sendLogisticsCompany = $("#logisticsCompany").find("option:selected").text();
             var params = {};
             params.id = id;
             params.token = getCookie('token');
             params.sendLogisticsCompany = sendLogisticsCompany;
             params.sendLogisticsCode = sendLogisticsCode;
             params.sendLogisticsNo = sendLogisticsNo;
        
            $.post('/antique/api/mallConsignOrder/consign_update_detail', params, function (result) {
                if (result.code == 0) {
                    myDeliver(id);
                } else {
                    $.toast(result.message, "cancel");
                }
                });
         } },
        { text: "取消", className: "default" }
      ]
    });
  }